import {AxdrType} from "../asn1.axdr/AxdrType";
import {ReverseByteArrayInputStream} from "../ReverseByteArrayInputStream";
import {ReverseByteArrayOutputStream} from "../ReverseByteArrayOutputStream";
import {Buffer} from "buffer";
import {OMD} from "./OMD";
import {Data} from "./Data";
import {OAD} from "./OAD";
import {Unsigned8} from "./Unsigned8";


export class ProxyActionThenGet implements AxdrType{

    dataCode : Buffer | null = null;
    omd : OMD | null = null;
    param : Data | null = null;
    oad : OAD | null = null;
    delayTimeRead : Unsigned8 | null = null;

    constructor() {
    }

    set_dataCode(omd : OMD, param : Data, oad : OAD, delayTimeRead : Unsigned8){
        this.omd = omd;
        this.param = param;
        this.oad = oad;
        this.delayTimeRead = delayTimeRead;
    }

    setOmd(omd : OMD){
        this.omd = omd;
    }

    setParam(param : Data){
        this.param = param;
    }

    setOad(oad : OAD){
        this.oad = oad;
    }

    setDelayTimeRead(delayTimeRead : Unsigned8){
        this.delayTimeRead = delayTimeRead;
    }



    decode(input: ReverseByteArrayInputStream): number {
        let codeLength = 0;

        this.omd = new OMD();
        codeLength += this.omd.decode(input);

        this.param = new Data();
        codeLength += this.param.decode(input);

        this.oad = new OAD();
        codeLength += this.oad.decode(input);

        this.delayTimeRead = new Unsigned8();
        this.delayTimeRead.set_const();
        codeLength += this.delayTimeRead.decode(input);

        return codeLength;
    }

    encode(output: ReverseByteArrayOutputStream): number {
        let codeLength;

        if (this.dataCode != null) {
            codeLength = this.dataCode.length;
            for (let i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
        } else {
            if(this.omd != null && this.oad != null && this.param != null && this.delayTimeRead != null){
                codeLength = 0;
                codeLength += this.delayTimeRead.encode(output);
                codeLength += this.oad.encode(output);
                codeLength += this.param.encode(output);
                codeLength += this.omd.encode(output);
            }else {
                throw new Error("delayTimeRead || oad || param || omd is null");
            }
        }
        return codeLength;
    }

    encodeAndSave(encodingSizeGuess: number) {
        let revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    toString(): string {
        if (this.param != null && this.omd != null && this.oad != null && this.delayTimeRead != null) {
            return "sequence: {"+ "omd: " + this.omd + ", param: " + this.param + ", oad: " + this.oad + ", delaytimeread: " + this.delayTimeRead  + "}";
        } else {
            return "delayTimeRead || oad || param || omd is null";
        }
    }

}
